package com.tianqu.tms.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.AucBidhistoryDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.AucBidhistoryDto;

/**
 * 
 * 竞价历史表Entity
 * 
 * @author tianshifu
 * 
 */
public class AucBidhistory extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3195522489826676067L;

	private int id; 			// 主键ID
	private int aucMagId; 		// 竞价车辆ID tms_auc_management.id
	private String memberId; 	// 会员ID tms_member.memberId
	private String bidTime; 	// 出价时间 yyyy-mm-dd hh:mm:ss
	private int price; 			// 出价金额 单位:（元）
   
	public static AucBidhistory getInstance(){
		AucBidhistory auc = new AucBidhistory();
		auc.setExisting(false);
		return auc;
	}
	
	
	/**
	 * 
	 * 根据会员ID查询车辆竞价信息
	 * @throws TmsDaoException 
	 */
	public List<AucBidhistory> findAucHistoryInfoByAucId(int memberId,int aucMagId) throws TmsDaoException
	{
		AucBidhistoryDao dao = (AucBidhistoryDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDHISTORY);
				
		List<AucBidhistoryDto> dtoList= dao.findAucHistoryByAucId(memberId,aucMagId);
		
		List<AucBidhistory>  entityList = new ArrayList<AucBidhistory>();
		AucBidhistory entity = null;
		if(dtoList.size()>0)
		{
			for(int i=0;i<dtoList.size();i++)
			{
				AucBidhistoryDto dto = dtoList.get(i);
				entity = new AucBidhistory();
				entity = setEntityFromDto(entity, dto);
				entityList.add(entity);
			}
		}
		
		return entityList;
	}
	/**
	 * 
	 * 将dto信息set到entity中
	 */
	private AucBidhistory setEntityFromDto(AucBidhistory entity,AucBidhistoryDto dto)
	{		
		entity.setId(dto.getId());
		entity.setAucMagId(dto.getAucMagId());
		entity.setMemberId(dto.getMemberId());
		entity.setBidTime(dto.getBidTime());
		entity.setPrice(dto.getPrice());
		
		return entity;
	}
	/**
	 * 
	 * 根据竞价管理表主键ID查询车辆出价信息
	 * @throws TmsDaoException 
	 */
	public List<AucBidhistory> getMaxAucBidInfoByAucId(int aucId) throws TmsDaoException
	{
		AucBidhistoryDao dao = (AucBidhistoryDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDHISTORY);
			
		List<AucBidhistoryDto> dtoList= dao.getAucBidInfoByAucId(aucId);
		
		List<AucBidhistory>  entityList = new ArrayList<AucBidhistory>();
		AucBidhistory entity = null;
		if(dtoList.size()>0)
		{
			for(int i=0;i<dtoList.size();i++)
			{
				AucBidhistoryDto dto = dtoList.get(i);
				entity = new AucBidhistory();
				entity = setEntityFromDto(entity, dto);
				entityList.add(entity);
			}
		}
		
		return entityList;
	}
	/**
	 * 
	 * 根据竞价管理表主键ID查询车辆出价信息
	 * 
	 * 按最高出价排序
	 * 
	 * @throws TmsDaoException 
	 */
	public List<AucBidhistory> getMaxAucBidInfoByAucIdAndPrice(int aucId) throws TmsDaoException
	{
		AucBidhistoryDao dao = (AucBidhistoryDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDHISTORY);
			
		List<AucBidhistoryDto> dtoList= dao.getAucBidInfoByAucIdAndPrice(aucId);
		
		List<AucBidhistory>  entityList = new ArrayList<AucBidhistory>();
		AucBidhistory entity = null;
		if(dtoList.size()>0)
		{
			for(int i=0;i<dtoList.size();i++)
			{
				AucBidhistoryDto dto = dtoList.get(i);
				entity = new AucBidhistory();
				entity = setEntityFromDto(entity, dto);
				entityList.add(entity);
			}
		}
		
		return entityList;
	}
	
	/**
	 * 
	 * 保存出价
	 * @throws TmsDaoException 
	 */
	public void saveAucHisInfo(AucBidhistory aucHis) throws TmsDaoException
	{
		AucBidhistoryDao dao = (AucBidhistoryDao) TmsDao.getInstance(TmsDao.TMS_AUC_BIDHISTORY);
		AucBidhistoryDto dto = new AucBidhistoryDto();
		dto = setDtoFromEntity(dto,aucHis);
		
		dao.saveAucHisInfoByDto(dto);
	}
	/**
	 * 
	 * 将entity数据set到dto中
	 */
	private AucBidhistoryDto setDtoFromEntity(AucBidhistoryDto dto,AucBidhistory entity)
	{
		dto.setAucMagId(entity.getAucMagId());
		dto.setMemberId(entity.getMemberId());
		dto.setPrice(entity.getPrice());		
		return dto;
	}
	
	@Override
	public void save() throws TmsDaoException, TmsException {

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getAucMagId() {
		return aucMagId;
	}

	public void setAucMagId(int aucMagId) {
		this.aucMagId = aucMagId;
	}

	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}


	public String getBidTime() {
		return bidTime;
	}


	public void setBidTime(String bidTime) {
		this.bidTime = bidTime;
	}


	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

}
